package no.weather.weatherProxy.wsKlima;

import java.security.InvalidParameterException;

import no.weather.WeatherType;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class WsKlimaDatabase {
	private static final String DATABASE_NAME = "wsklima.db";

	private static final int DATABASE_VERSION = 2;
	private static final String TAG = "WSKlimaDatabase";
	private static final String TYPE_CODE = "code";
	private static final String TYPE_EN_DESCRIPTION = "en_description";
	private static final String TYPE_EN_NAME = "en_name";
	private static final String TYPE_NN_DESCRIPTION = "nn_description";
	private static final String TYPE_NN_NAME = "nn_name";
	private static final String TYPE_NO_DESCRIPTION = "no_description";
	private static final String TYPE_NO_NAME = "no_name";
	private static final String TYPE_TABLE_NAME = "type";
	private static final String TYPE_UNIT = "unit";

	private final WsKlimaDatabaseOpenHelper database;

	public WsKlimaDatabase(Context context) {
		database = new WsKlimaDatabaseOpenHelper(context);
	}

	/**
	 * Adds or update a weather type in the database
	 * 
	 * @param type
	 * @param language
	 */
	public void add(WeatherType type, String language) {
		ContentValues value = new ContentValues();
		value.put(TYPE_CODE, type.getCode());
		value.put(TYPE_UNIT, type.getUnit());
		if (language.equals("EN")) {
			value.put(TYPE_EN_DESCRIPTION, type.getDescription());
			value.put(TYPE_EN_NAME, type.getName());
		} else if (language.equals("NN")) {
			value.put(TYPE_NN_DESCRIPTION, type.getDescription());
			value.put(TYPE_NN_NAME, type.getName());
		} else if (language.equals("NO")) {
			value.put(TYPE_NO_DESCRIPTION, type.getDescription());
			value.put(TYPE_NO_NAME, type.getName());
		} else
			throw new InvalidParameterException(language
					+ " is not suported, or not in uppercase");
		String where = TYPE_CODE + "='" + type.getCode() + "'";
		SQLiteDatabase db = database.getReadableDatabase();
		Cursor c = db.query(TYPE_TABLE_NAME, new String[] { TYPE_CODE }, where,
				null, null, null, null);
		db.close();

		if (c.getCount() > 0) {
			// Update database
			database.getWritableDatabase().update(TYPE_TABLE_NAME, value,
					where, null);
		} else {
			database.getWritableDatabase().insert(TYPE_TABLE_NAME, null, value);
		}

		c.close();
	}

	/**
	 * Fill out the weather type object
	 * 
	 * @param Element
	 *            to be filled out
	 * @param language
	 *            , NO, NN or EN
	 * @throws WeatherTypeNotFoundException
	 *             if not in database
	 */
	public void fill(final WeatherType type, String language)
			throws WeatherTypeNotFoundException {
		String columns[] = null;
		if (language.equals("NO")) {
			columns = new String[] { TYPE_NO_DESCRIPTION, TYPE_NO_NAME,
					TYPE_UNIT };
		} else if (language.equals("NN")) {
			columns = new String[] { TYPE_NN_DESCRIPTION, TYPE_NN_NAME,
					TYPE_UNIT };
		} else if (language.equals("EN")) {
			columns = new String[] { TYPE_EN_DESCRIPTION, TYPE_EN_NAME,
					TYPE_UNIT };
		} else
			throw new InvalidParameterException(language
					+ " is not suported, or not in uppercase");
		String where = TYPE_CODE + "='" + type.getCode() + "'";
		SQLiteDatabase db = database.getReadableDatabase();
		Cursor c = db.query(TYPE_TABLE_NAME, columns, where, null, null, null,
				null);

		if (c == null || c.getCount() == 0)
			throw new WeatherTypeNotFoundException();

		c.moveToFirst();
		String name = null, description = null;
		String unit = c.getString(c.getColumnIndexOrThrow(TYPE_UNIT));
		if (language.equals("NO")) {
			name = c.getString(c.getColumnIndexOrThrow(TYPE_NO_NAME));
			description = c.getString(c.getColumnIndex(TYPE_NO_DESCRIPTION));
		} else if (language.equals("NN")) {
			name = c.getString(c.getColumnIndexOrThrow(TYPE_NN_NAME));
			description = c.getString(c.getColumnIndex(TYPE_NN_DESCRIPTION));
		} else if (language.equals("EN")) {
			name = c.getString(c.getColumnIndexOrThrow(TYPE_EN_NAME));
			description = c.getString(c.getColumnIndex(TYPE_EN_DESCRIPTION));
		}

		c.close();
		db.close();
		if (name == null || description == null)
			throw new WeatherTypeNotFoundException();

		type.setUnit(unit);
		type.setName(name);
		type.setDescription(description);

	}

	@Override
	protected void finalize() throws Throwable {
		database.close();
		super.finalize();
	}

	private class WsKlimaDatabaseOpenHelper extends SQLiteOpenHelper {
		private static final String TYPE_TABLE_CREATE = "CREATE TABLE "
				+ TYPE_TABLE_NAME + " (" + TYPE_CODE + " TEXT," + TYPE_UNIT
				+ " TEXT," + TYPE_NO_NAME + " TEXT," + TYPE_NO_DESCRIPTION
				+ " TEXT," + TYPE_NN_NAME + " TEXT," + TYPE_NN_DESCRIPTION
				+ " TEXT," + TYPE_EN_NAME + " TEXT," + TYPE_EN_DESCRIPTION
				+ " TEXT)";

		public WsKlimaDatabaseOpenHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		public WsKlimaDatabaseOpenHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(TYPE_TABLE_CREATE);
			Log.v(TAG, "Create database: " + DATABASE_NAME);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS " + TYPE_TABLE_NAME);
			onCreate(db);

		}

	}

}
